/*
 * @Author: NJ_1098 xiabaoxin
 * @Date: 2025-02-13 17:21:12
 * @LastEditors: NJ_1098 xiabaoxin
 * @LastEditTime: 2025-02-19 17:08:06
 * @Description: 账单中间件
 */
const { optionLackError, optionError } = require("../constant");
const BaseMiddle = require("./base.middle");
const baseMiddleInstance = new BaseMiddle();

class BillMiddleware {
  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 新增必填参数校验
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async addOptionValidate(ctx, next) {
    if (!(await baseMiddleInstance.optionValidate(ctx))) {
      return;
    }
    const { name } = ctx.request.body;
    if (!name) {
      ctx.app.emit("error", optionLackError, ctx);
      return;
    }
    await next();
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 删除必传参数校验
   * @return {*}
   */
  static async deleteValidate(ctx, next) {
    if (!(await baseMiddleInstance.optionValidate(ctx))) {
      return;
    }
    const { bids } = ctx.request.body;
    if (!bids || bids.length === 0) {
      ctx.app.emit("error", optionError, ctx);
      return;
    }
    await next();
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 详情必传参数校验
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async detailValidate(ctx, next) {
    if (!(await baseMiddleInstance.optionValidate(ctx))) {
      return;
    }
    const { bid } = ctx.request.query;
    if (!bid) {
      ctx.app.emit("error", optionError, ctx);
      return;
    }
    await next();
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 账单更新参数校验
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async updateValidate(ctx, next) {
    if (!(await baseMiddleInstance.optionValidate(ctx))) {
      return;
    }
    const { bid } = ctx.request.body;
    if (!bid) {
      ctx.app.emit("error", optionError, ctx);
      return;
    }
    await next();
  }
}

module.exports = BillMiddleware;
